package cn.com.xjnt.vas.web.ordering.api;

import java.util.List;

import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.frameworks.core.web.controller.BaseController;
import com.frameworks.core.web.result.DataResult;
import com.google.common.collect.Lists;

import cn.com.xjnt.vas.ordering.domain.entity.Department;
import cn.com.xjnt.vas.ordering.domain.entity.Hospital;
import cn.com.xjnt.vas.ordering.domain.vo.DepartmentVo;
import cn.com.xjnt.vas.ordering.service.DepartmentService;
import cn.com.xjnt.vas.ordering.service.HospitalService;

/**
 * 类说明：
 * 
 * @author 作者 LUXIN
 * @version 创建时间：2016年5月25日17:39:06
 */
@Controller
@RequestMapping("/api/department")
public class DepartmentAPI extends BaseController {
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private Mapper mapper;

    @RequestMapping(value = "/get", method = { RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
    @ResponseBody
    public DataResult get(@RequestBody JSONObject json) {
        DataResult dataRet = new DataResult();
        try {
            Long id = json.getLong("id");
            Department department = departmentService.findById(id);
            if(department != null){
                DepartmentVo vo = mapper.map(department, DepartmentVo.class);
                dataRet.setRows(vo);
                dataRet.pushOk("获取科室信息成功");
            }else{
                dataRet.pushError("科室信息不存在请确认");
            }
        } catch (Exception e) {
            dataRet.pushError("获取科室信息失败");
            logger.error("获取科室信息错误", e);
        }
        return dataRet;
    }
    
    @RequestMapping(value = "/find", method = { RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
    @ResponseBody
    public DataResult find(@RequestBody JSONObject json) {
        DataResult dataRet = new DataResult();
        try {
            Long hospitalId = json.getLong("hospitalId");
            Hospital hospital = hospitalService.findById(hospitalId);
            if(hospital != null){
                List<Department> lists = departmentService.findByHospital(hospital);
                List<DepartmentVo> lvos = Lists.newArrayList();
                for (Department department : lists) {
                    DepartmentVo vo = mapper.map(department, DepartmentVo.class);
                    lvos.add(vo);
                }
                if (lvos.size() == 0) {
                    dataRet.getExtra().put("type", 0);
                    dataRet.pushOk("无数据");
                } else {
                    dataRet.getExtra().put("type", 1);
                    dataRet.pushOk("查询成功");
                }
                dataRet.setRows(lvos);
            }else{
                dataRet.getExtra().put("type", 0);
                dataRet.pushOk("医院不存在");
            }
        } catch (Exception e) {
            dataRet.pushError("获取科室列表信息失败");
            logger.error("获取科室列表信息错误", e);
        }
        return dataRet;
    }
    
    
}
